﻿using Microsoft.Practices.Unity;
using UnityDemo.LoggingUtility;
using UnityDemo.Models;

namespace UnityDemo.RegAndResCode
{
    internal class ConstructResolutionSample : ISample
    {
        public ConstructResolutionSample()
        {
            InterfacesResolutionViaClassContructor();
        }

        /// <summary>
        /// This sample shows how to Register an interface and its implementing class automatically pass an instance
        /// as a constructor paramerter
        /// </summary>
        private void InterfacesResolutionViaClassContructor()
        {
            var container = new UnityContainer();

            //Registering IDuck and its implementing class. This registration is a Default registration
            //Soldier has a constructor which need a IQuackStrategy type instance
            container.RegisterType<IDuck, SoldierDuck>();

            //Registering IDuck and its implementing class. This registration is a Default registration
            container.RegisterType<IQuackStrategy, LoudQuack>();

            //Resolving instance of IDuck
            var duck = container.Resolve<IDuck>();

            LogerInstance.Logger.Write(duck);

            LogerInstance.Logger.Write(duck.QuackType.Quack());
        }

        /// <summary>
        /// This sample shows how to Register an interface and its implementing class automatically pass an instance
        /// as a constructor paramerter
        /// </summary>
        private void InterfacesAndClassDemo()
        {
            var container = new UnityContainer();

            //Registering IDuck and its implementing class. This registration is a Default registration
            container.RegisterType<IDuck, SoldierDuck>();
            IQuackStrategy quack = new LoudQuack();
            //Registering IDuck and its implementing class. This registration is a Default registration
            // container.RegisterType<IQuackStrategy, LoudQuack>();

            //Resolving instance of IDuck
            var duck = container.Resolve<IDuck>(quack.Quack());

            LogerInstance.Logger.Write(duck);

            LogerInstance.Logger.Write(duck.QuackType.Quack());
        }
    }
}